<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="申请人" prop="customerNameChinese">
                    <el-input class="w-[280px]" v-model="queryParams.customerNameChinese" />
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                    <el-input class="w-[280px]" v-model="queryParams.customerName" />
                </el-form-item>
                <el-form-item label="申请人地址" prop="streetChinese">
                    <el-input class="w-[280px]" v-model="queryParams.streetChinese" />
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                    <daterange-picker
                        v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="国家(地区)" prop="nation">
                    <el-input class="w-[280px]" v-model="queryParams.nation" />
                </el-form-item>
                <el-form-item label="是否监控年费" prop="isAnnualMonitoringFee">
                    <el-select
                        v-model="queryParams.isAnnualMonitoringFee"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="是" value="1" />
                        <el-option label="否" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="客户业务员" prop="updateUserName">
                    <el-select v-model="queryParams.updateUserName" class="w-[280px]" clearable>
                        <el-option label="流程01" value="1" />
                        <el-option label="流程02" value="2" />s
                    </el-select>
                </el-form-item>
                <el-form-item label="申请人编号" prop="applicantId">
                    <el-input class="w-[280px]" v-model="queryParams.applicantId" />
                </el-form-item>
                <el-form-item label="申请人类型" prop="applicantType">
                    <el-select v-model="queryParams.applicantType" class="w-[280px]" clearable>
                        <el-option label="科研单位" value="1" />
                        <el-option label="工矿企业" value="2" />
                        <el-option label="事业单位" value="3" />
                        <el-option label="个人" value="4" />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否费减" prop="feeReductionFiling">
                    <el-select v-model="queryParams.feeReductionFiling" class="w-[280px]" clearable>
                        <el-option label="是" value="1" />
                        <el-option label="否" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="费减开始日期" prop="reductionCreateTime">
                    <daterange-picker
                        v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="费减结束日期" prop="reductionEndTime">
                    <daterange-picker
                        v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd"
                    />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['applicant:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" min-width="50" />
                <el-table-column label="客户名称" prop="customerName" min-width="180" />
                <el-table-column label="申请人(中文)" prop="customerNameChinese" min-width="180" />
                <el-table-column label="申请人(英文)" prop="customerNameEnglish" min-width="180" />
                <el-table-column label="申请人编号" prop="applicantId" min-width="180" />
                <el-table-column label="所属地区" prop="nation" min-width="180" />
                <el-table-column label="申请人地址" prop="streetChinese" min-width="180" />
                <el-table-column label="联系电话" prop="phone" min-width="180" />
                <el-table-column label="邮件" prop="email" min-width="180" />
                <el-table-column label="第一联系人" prop="isFirstContact" min-width="180" />
                <el-table-column label="客户业务员" prop="updateUserName" min-width="180" />
                <el-table-column label="案件类型" prop="caseType" min-width="180" />

                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['applicant:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['applicant:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="applicant">
import { applicantDelete, applicantLists } from '@/api/applicant'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    customerName: '',
    applicantType: '',
    customerNameChinese: '',
    applicantId: '',
    streetChinese: '',
    feeReductionFiling: '',
    reductionCreateTimeStart: '',
    reductionCreateTimeEnd: '',
    reductionEndTimeStart: '',
    reductionEndTimeEnd: '',
    nation: '',
    isAnnualMonitoringFee: '',
    updateUserName: '',
    createTimeStart: '',
    createTimeEnd: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: applicantLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await applicantDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
